﻿//初始化主菜单
function initMainNav(id) {
    if (id == 0) {
        $("#l_index a").addClass("active");
    } else if (id == 1) {
        $("#l_user_manager a").addClass("active");
    } else if (id == 2) {
        $("#l_resource_manager a").addClass("active");
    } else if (id == 3) {
        $("#l_system_manager a").addClass("active");
    }
    else if (id == 4) {
        $("#l_admin_manager a").addClass("active");
    }
}

//添加成员
function addUser() {
    var name = $("#name").val();
    var username = $("#username").val();
    var password = $("#password").val();
    var password2 = $("#password2").val();
    var departmentId = $("#departmentId").val();
    if (name == "") {
        alert("姓名不能为空");
        return;
    }
    if (username == "") {
        alert("邮箱不能为空");
        return;
    }
    if (!isValidMail(username)) {
        alert("邮箱格式不正确");
        return;
    }
    if (password == "") {
        alert("密码不能为空");
        return;
    }
    if (password2 != password) {
        alert("二次密码不一致");
        return;
    }
    if (departmentId == "0") {
        alert("必须选择部门");
        return;
    }
    $.ajax({
        type: "POST",
        url: "adduser.aspx/ajaxAddUser",
        data: "{name:'" + name + "',username:'" + username + "',password:'" + password + "',departmentId:" + departmentId + "}",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                alert(msg);
                $("#name").val("");
                $("#username").val("");
                $("#password").val("");
                $("#password2").val("");
            } else {
                alert("添加失败：" + msg);
            }
        }
    });
}

//邮箱地址格式校验
function isValidMail(sText) {
    var reMail = /^(?:\w+\.?)*\w+@(?:\w+\.?)*\w+$/;
    return reMail.test(sText);
}

//添加部门
function addDepartment() {
    var name = $("#name").val().trim();
    if (name == "") {
        alert("部门名称不能为空");
        return;
    }
    $.ajax({
        type: "POST",
        url: "department.aspx/ajaxAddDepartment",
        data: "{name:'" + name + "'}",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                $("#name").val("");
                alert(msg);
                location.reload();
            } else {
                alert("添加失败：" + msg);
            }
        }
    });
}

//修改部门
function modifyDepartment(id, name) {
    var newName = prompt("更改部门", name).trim();
    if (newName == null || newName == "")
        return;
    if (newName == name)
        return;
    $.ajax({
        type: "POST",
        url: "department.aspx/ajaxModifyDepartment",
        data: "{id:" + id + ",newName:'" + newName + "'}",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                alert(msg);
                location.reload();
            } else {
                alert("更改失败：" + msg);
            }
        }
    });
}

//初始化部门下拉框
function initDepartment() {
    $.ajax({
        type: "POST",
        data: "{id:1}",
        url: "department.aspx/ajaxGetDepartmentList",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var ds = info.result.split("|");
            var selectStr = "<select id='departmentId'>" +
                            "<option value='0'></option>";
            for (var i = 0; i < ds.length - 1; i++) {
                var item = ds[i];
                var id = item.split("#")[0];
                var name = item.split("#")[1];
                selectStr += "<option value ='" + id + "'>" + name + "</option>";
            }
            selectStr += "</select>";
            $("#departmentList").html("所属部门：" + selectStr);
        }
    });
}

//冻结账号
function freezeUser(id) {
    if (id == null || id == "")
        return;
    $.ajax({
        type: "POST",
        data: "{id:" + id + "}",
        url: "userList.aspx/freezeUser",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                alert(msg);
                location.reload();
            } else {
                alert("冻结账号失败：" + msg);
            }
        }
    });
}

//恢复账号
function recoverUser(id) {
    if (id == null || id == "")
        return;
    $.ajax({
        type: "POST",
        data: "{id:" + id + "}",
        url: "userList.aspx/recoverUser",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                alert(msg);
                location.reload();
            } else {
                alert("恢复账号失败：" + msg);
            }
        }
    });
}

//添加管理员
function addAdmin() {
    var userId = $("#userId").val().trim();
    if (userId == null || userId == "")
        return;
    $.ajax({
        type: "POST",
        data: "{userId:'" + userId + "'}",
        url: "admin.aspx/ajaxAddAdmin",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                alert(msg);
                location.reload();
            } else {
                alert("添加管理员失败：" + msg);
            }
        }
    });
}

//修改权限
function modifyUserAction(userId, checkId) {
    var action1 = "0";
    var action2 = "0";
    var action3 = "0";
    if ($("#" + checkId + "1").attr("checked") == true)
        action1 = "1";
    if ($("#" + checkId + "2").attr("checked") == true)
        action2 = "1";
    if ($("#" + checkId + "3").attr("checked") == true)
        action3 = "1";
    var actionStr = action1 + "#" + action2 + "#" + action3;
    $.ajax({
        type: "POST",
        data: "{userId:" + userId + ",actionStr:'" + actionStr + "'}",
        url: "admin.aspx/ajaxModifyAdminAction",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                alert(msg);
                location.reload();
            } else {
                alert("更改权限失败：" + msg);
            }
        }
    });
}

//删除管理员
function deleteAdmin(userId) {
    if (!confirm("确定删除该管理员?"))
        return;
    $.ajax({
        type: "POST",
        data: "{userId:" + userId + "}",
        url: "admin.aspx/ajaxDeleteAdmin",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                location.reload();
            } else {
                alert("删除管理员：" + msg);
            }
        }
    });
}


//发布公告
function publishNotice() {
    var content = $("#content").val();
    var type = $("#toWho").val();
    if (content == null || content == "")
        return;
    $.ajax({
        type: "POST",
        data: "{content:'" + content + "',type:" + type + "}",
        url: "system.aspx/publishNotice",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                $("#content").val("");
                alert("发布成功");
            } else {
                alert("发布失败：" + msg);
            }
        }
    });
}


//添加分类
function addCategory() {
    var categoryOrder = $("#categoryOrder").val().trim();
    var categoryName = $("#categoryName").val().trim();
    var chineseName = $("#chineseName").val().trim();

    if (categoryOrder == null || categoryOrder == "") {
        alert("请输入序号");
        retutrn;
    }
    if (categoryName == null || categoryName == "") {
        alert("请输入英文名称");
        retutrn;
    }
    if (chineseName == null || chineseName == "") {
        alert("请输入中文名称");
        retutrn;
    }
    $.ajax({
        type: "POST",
        data: "{categoryOrder:" + categoryOrder + ",categoryName:'" + categoryName + "',chineseName:'" + chineseName + "'}",
        url: "category.aspx/ajaxAddCategory",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                $("#content").val("");
                location.reload();
            } else {
                alert("添加失败：" + msg);
            }
        }
    });
}

//修改分类中文名称
function modifyCategoryChineseName(id, name) {
    var newName = prompt("更改分类", name).trim();
    if (newName == null || newName == "")
        return;
    if (newName == name)
        return;
    $.ajax({
        type: "POST",
        url: "category.aspx/modifyCategoryChineseName",
        data: "{id:" + id + ",newName:'" + newName + "'}",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                location.reload();
            } else {
                alert("更改失败：" + msg);
            }
        }
    });
}

//修改分类顺序
function modifyCategoryOrder(id, order) {
    var newOrder = prompt("更改顺序", order).trim();
    if (order == null || order == "")
        return;
    if (newOrder == order)
        return;
    if (!isNumber(newOrder) || newOrder <= 0) {
        alert("请输入大于0的数字");
        return;
    }
    $.ajax({
        type: "POST",
        url: "category.aspx/modifyCategoryOrder",
        data: "{id:" + id + ",newOrder:" + newOrder + "}",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                location.reload();
            } else {
                alert("更改失败：" + msg);
            }
        }
    });
}

//验证数字
function isNumber(oNum) {
    if (!oNum) return false;
    var strP = /^\d+(\.\d+)?$/;
    if (!strP.test(oNum)) return false;
    try {
        if (parseFloat(oNum) != oNum) return false;
    }
    catch (ex) {
        return false;
    }
    return true;
}

//初始化资源管理侧边栏
function initResourceSideNav() {
    var args = new Object();
    args = getUrlParms();
    var type = args["type"];
    if (type == "all") {
        $("#l_all_resource a").addClass("active");
        $("#l_unaudited_resource a").removeClass("active");
        $("#l_audited_resource a").removeClass("active");
    }
    else if (type == "unaudited") {
        $("#l_all_resource a").removeClass("active");
        $("#l_unaudited_resource a").addClass("active");
        $("#l_audited_resource a").removeClass("active");
    }
    else if (type == "audited") {
        $("#l_all_resource a").removeClass("active");
        $("#l_unaudited_resource a").removeClass("active");
        $("#l_audited_resource a").addClass("active");
    }
    else {
        $("#l_all_resource a").addClass("active");
        $("#l_unaudited_resource a").removeClass("active");
        $("#l_audited_resource a").removeClass("active");
    }
}

//得到get参数
function getUrlParms() {
    var args = new Object();
    var query = location.search.substring(1);         //获取查询串   
    var pairs = query.split("&");                              //在逗号处断开   
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');                      //查找name=value   
        if (pos == -1) continue;                            //如果没有找到就跳过   
        var argname = pairs[i].substring(0, pos); //提取name   
        var value = pairs[i].substring(pos + 1);      //提取value   
        args[argname] = unescape(value);         //存为属性   
    }
    return args;
}

//通过审核
function auditedResource(id) {
    $.ajax({
        type: "POST",
        url: "resourceList.aspx/ajaxAuditedResource",
        data: "{id:" + id + "}",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                location.reload();
            } else {
                alert("审核失败：" + msg);
            }
        }
    });
}

//通过拒绝
function unAuditedResource(id) {
    var reason = prompt("拒绝理由", "").trim();
    if (reason == null || reason == "") {
        alert("请输入拒绝理由");
        return;
    }
    $.ajax({
        type: "POST",
        url: "resourceList.aspx/ajaxUnAuditedResource",
        data: "{id:" + id + ",reason:'" + reason + "'}",
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            var dataObj = eval("(" + data + ")");
            var info = eval("(" + dataObj.d + ")");
            var suc = info.result;
            var msg = info.message;
            if (suc == 1) {
                location.reload();
            } else {
                alert("审核失败：" + msg);
            }
        }
    });
}